#include "gpioconfig.h"
#include "update.h"
#include "config.h"
#include "bsp_uart.h"
#include "bsp_pthread.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>
#include "bsp_mjpeg.h"
//ffmpeg
extern "C"{
#include <libavdevice/avdevice.h>
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libswscale/swscale.h>
#include <libavutil/avutil.h>
#include <libavutil/opt.h>
}
//1 mcu
Uart_t* g_uart_mcu = nullptr;          // 串口上下文指针
pthread_mutex_t mcu_data_lock = PTHREAD_MUTEX_INITIALIZER;  //静态锁，针对串口mcu的串口；读数据保护，写数据保护

//2 laser
Uart_t* g_uart_laser = nullptr;          // 串口上下文指针
//3 红外
Uart_t* g_uart_icr = nullptr;          // 串口上下文指针
//4 可见
Uart_t* g_uart_vis = nullptr;          // 串口上下文指针
//5 镜头
Uart_t* g_uart_lens = nullptr;          // 串口上下文指针
//6 RS422传输数据口
Uart_t* g_uart_usb0 = nullptr;          // 串口上下文指针
pthread_mutex_t usb0_data_lock = PTHREAD_MUTEX_INITIALIZER;  //静态锁，针对串口usb0的串口；读数据保护，写数据保护

pthread_mutex_t select_video_lock = PTHREAD_MUTEX_INITIALIZER;  //静态锁，针对串口usb0的串口；读数据保护，写数据保护
int main()
{
    /*01 sysfs init: en*/
    McuBspInit();
    LaserBspInit();
    avdevice_register_all();
    avformat_network_init();
    /*02 init uart fds*/
    int ret = -1; 
    int flags = O_RDWR | O_NOCTTY | O_NONBLOCK;  // 非阻塞模式
        //1 mcu
    g_uart_mcu = uart_init_context("/dev/ttyS9", 115200, 8, 1, 'N');
    if (!g_uart_mcu) {
        printf("错误: 串口上下文初始化失败\n");
        return 1;
    }
    if (open_and_configure_uart(g_uart_mcu, flags) < 0) {
        printf("错误: 串口配置失败\n");
        uart_free_context(g_uart_mcu);
        return 1;
    }
    uart_show_data(g_uart_mcu);
        //2 laser
    g_uart_laser = uart_init_context("/dev/ttyS0", 38400, 8, 1, 'N');
    if (!g_uart_laser) {
        printf("错误: 串口上下文初始化失败\n");
        return 1;
    }
    if (open_and_configure_uart(g_uart_laser, flags) < 0) {
        printf("错误: 串口配置失败\n");
        uart_free_context(g_uart_laser);
        return 1;
    }
    uart_show_data(g_uart_laser);
        //3 icr
    g_uart_icr = uart_init_context("/dev/ttyS5", 115200, 8, 1, 'E');
    if (!g_uart_icr) {
        printf("错误: 串口上下文初始化失败\n");
        return 1;
    }
    if (open_and_configure_uart(g_uart_icr, flags) < 0) {
        printf("错误: 串口配置失败\n");
        uart_free_context(g_uart_icr);
        return 1;
    }
    uart_show_data(g_uart_icr);
        //4 vis
    g_uart_vis = uart_init_context("/dev/ttyS7", 9600, 8, 1, 'N');
    if (!g_uart_vis) {
        printf("错误: 串口上下文初始化失败\n");
        return 1;
    }
    if (open_and_configure_uart(g_uart_vis, flags) < 0) {
        printf("错误: 串口配置失败\n");
        uart_free_context(g_uart_vis);
        return 1;
    }
    uart_show_data(g_uart_vis);
        //5 lens
    g_uart_lens = uart_init_context("/dev/ttyS4", 9600, 8, 1, 'N');
    if (!g_uart_lens) {
        printf("错误: 串口上下文初始化失败\n");
        return 1;
    }
    if (open_and_configure_uart(g_uart_lens, flags) < 0) {
        printf("错误: 串口配置失败\n");
        uart_free_context(g_uart_lens);
        return 1;
    }
    uart_show_data(g_uart_lens);
        //6 vencusb0
    g_uart_usb0 = uart_init_context("/dev/ttyUSB0", 4000000, 8, 1, 'N');
    if (!g_uart_usb0) {
        printf("错误: 串口上下文初始化失败\n");
        return 1;
    }
    if (open_and_configure_uart(g_uart_usb0, flags) < 0) {
        printf("错误: 串口配置失败\n");
        uart_free_context(g_uart_usb0);
        return 1;
    }
    uart_show_data(g_uart_usb0);


    uart_vis_init();
    /*03 init pthead mcu->soc data*/
    pthread_t rx_mcu_thd;  //轮询接收数据句柄
    pthread_t tx_mcu_thd;  //轮询发送数据句柄
    // pthread_t vis_thd_hdl;  // mjpeg线程句柄
    ret = pthread_create(&rx_mcu_thd, NULL, thd_read_mcu, NULL);
    if (ret != 0) {
        perror("Failed to create thd_read_mcu thread");
    }
    ret = pthread_create(&tx_mcu_thd, NULL, thd_send_mcu, NULL);
    if (ret != 0) {
        perror("Failed to create thd_read_mcu thread");
    }
    // ret = pthread_create(&vis_thd_hdl, NULL, encode_thread_mjpeg, mjpeg_encoder);
    // if (ret != 0) {
    //     perror("Failed to create thd_read_mcu thread");
    // }
    /*04 deinit pthread mcu->soc */
    pthread_join(rx_mcu_thd, NULL);
    pthread_join(tx_mcu_thd, NULL);

    // pthread_join(vis_thd_hdl, NULL);




    avformat_network_deinit();
    return 0;
}
    